package roadsuggester.tracks;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import roadsuggester.main.R;
import roadsuggester.main.Utils;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.location.Location;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.Projection;


//The track recorded by the user
public class RecordedTrack
{
	/**
	 * 
	 */
	private static final long serialVersionUID=1L;
	
	private List<Location> pointsForPath;
	private List<Long> timestamps;

	public RecordedTrack()
	{
		this.pointsForPath=new ArrayList<Location>();
		this.timestamps=new ArrayList<Long>();
	}

	public void addLocation(Location newLocation, long timestamp)
	{
		pointsForPath.add(newLocation);
		timestamps.add(timestamp);
	}
	public void addLocation(Location newLocation)
	{
		addLocation(newLocation,System.currentTimeMillis());
	}
	
	public long timeTook()
	{
		if (timestamps.size()<2)
		{
			return 0;
		}
		return timestamps.get(timestamps.size()-1)-timestamps.get(0);
	}
	public Location from()
	{
		if (pointsForPath.size()==0)
		{
			return null;
		}
		return pointsForPath.get(0);
	}
	public Location to()
	{
		if (pointsForPath.size()==0)
		{
			return null;
		}
		return pointsForPath.get(pointsForPath.size()-1);
	}
	
	public List<Location> getPath()
	{
		return pointsForPath;
	}
	
	public long timeAt(int locationIndex)
	{
		return timestamps.get(locationIndex);
	}

	public void drawTrack(Canvas canvas, Projection projection, int color)
	{
		if(pointsForPath.size()<2)
		{
			return;
		}
		Path path=new Path();

		for(int i=0; i<pointsForPath.size(); i++)
		{
			Location location=pointsForPath.get(i);
			GeoPoint geoPoint=Utils.locationToGeoPoint(location);
			Point pathPoint=new Point();
			projection.toPixels(geoPoint, pathPoint);
			if(i==0)
			{
				path.moveTo(pathPoint.x, pathPoint.y);
			}
			else
			{
				path.lineTo(pathPoint.x, pathPoint.y);
			}
		}
		Paint paint=new Paint();
		paint.setColor(color);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeWidth(3);

		canvas.drawPath(path, paint);
	}

	public void drawTrack(Canvas canvas, Projection projection)
	{
		drawTrack(canvas, projection, Color.BLUE);
	}

}
